package com.ws.shop.common;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;

import com.ws.shop.entity.Auction;
import com.ws.shop.entity.AuctionLog;
import com.ws.shop.entity.Member;
import com.ws.shop.service.impl.AuctionCacheImpl;
import com.ws.shop.util.CommonUtil;

public class CacheManager extends CacheSynLock<Long>{
	
	private static Logger log = Logger.getLogger(CacheManager.class);
	
    private Member member;
    private long curtime = 0; //当前时间
    
    public CacheManager(long time){
    	curtime = time;
    }
	public CacheManager(Member member){
		this.member = member;
	}
	
	public static Map<Long,Auction> getAllAcution(){
		return AuctionCacheImpl.auctionCache;
	}

	public String getValue(Long aid) {
		return null;
	}

	public void run() {
	
	}
	
	public Auction getAuction(){
		return null;
	}
	

	//获取正在拍的信息，本方法在返回之前,不同的ID值不会同步，相同的竞拍ID才会锁
	public Auction getAuctionByBid(Long aid) {
		Auction auction = AuctionCacheImpl.auctionCache.get(aid);
		if(auction == null){
			auction = new Auction();
			auction.setBidstate(3);//竞拍不存在
			return auction;
		}else if(auction.getState() == 0){//没有开始竞拍
			auction.setBidstate(4);
			return auction;
		}else if(auction.getState() == 2){//竞拍已经结束
			auction.setBidstate(5);
			return auction;
		}/*else if(auction.getType() == 1 && member.getOld() == 1){
			auction.setBidstate(6);
			return auction;
		}*/else if(auction.getUid() == member.getId()){ //最后竞拍者是自己
			auction.setBidstate(6);
			return auction;
		}
		
		if (auction.getIsdiscount() != 0){
			 if(auction.getBeforebout() !=0 && auction.getDiscount() != 0){//折扣计算
				 if(auction.getAuctioncount() <= auction.getBeforebout()){
					 auction.setUsebeat(auction.getUsebeat() * auction.getDiscount()/10);
				 }
			 }
		}
		//免费点，使用免费点，收费点就使用收费点，不采用优先规则使用规则member.getPriority()
		if(auction.getIsfree() == 0){
			if (member.getBeat() < auction.getUsebeat()){
				auction.setBidstate(7); //余额不足
				return auction;
			}
		}else{//免费体验
			if (member.getPointb() < auction.getUsebeat()){
				auction.setBidstate(7); //余额不足
				return auction;
			}
		}
		auction.setBidstate(0);
		//更新竞拍缓存
		long currenttime =  this.getCurrenttime(auction);
 		int auctioncount = auction.getAuctioncount() + 1;   //对数据库的竞拍数据更新
 		float currentprice = auction.getRealprice() + auction.getAddprice(); //当前价格
 		auction.setRealprice(currentprice);
 		auction.setAuctioncount(auctioncount);
 		auction.setEndtime(new Date(currenttime));
 		auction.setUid(member.getId());
 		auction.setAuctionuname(member.getName());
        auction.setUptime(new Date().getTime());
 		//更新竞拍日志缓存
 		updateCacheLog(auction,member);
		return auction;
	}
	
	//获取当前时间 
	public long getCurrenttime(Auction auction){
		long currenttime = 0;
 		if(auction.getMaxaddsecond()>0){
 			long time = new Date().getTime();
 			long diftime = auction.getEndtime().getTime()-time;
 			if(diftime < auction.getMaxaddsecond()){
 				currenttime = auction.getEndtime().getTime() + auction.getAddsecond(); //当前时间
 				if(currenttime-time > auction.getMaxaddsecond())currenttime= time+ auction.getMaxaddsecond();
 			}else{
 				currenttime = auction.getEndtime().getTime();
 			}
 		}else{
 			//currenttime = auction.getEndtime().getTime() + auction.getAddsecond(); //当前时间
 			currenttime = new Date().getTime() + auction.getAddsecond();
 		}
 		return currenttime;
	}
	
    
	
	//机器人拍,同时每次竞拍的信息;
	public Auction robotsbid(Long aid){
		Auction auction = AuctionCacheImpl.auctionCache.get(aid);
		if(auction != null){
			if(auction.getState() == 1){
				if (auction.getEndtime().getTime() < new Date().getTime()){
					if(auction.getRealprice() - auction.getRobotspromote() < auction.getTarget()){ //没有达到
					    long surplus = auction.getEndtime().getTime() - new Date().getTime();
						if (surplus < auction.getMax()){
							if (surplus <= auction.getMin()){
								robot(auction);
							}else {
								boolean bl = lucker();//触发概率
								if (bl){
									robot(auction);
								}
							}
						}
						if(auction.getRobotspromote() > 5 * auction.getTarget()){
							auction.setState(2);
							auction.setUptime(new Date().getTime());
							auction.setRequsttime(new Date().getTime());
							auction.setIsupdatestate(true);
						}
					}else{ //更新成功竞拍的数据
						auction.setState(2);
						auction.setUptime(new Date().getTime());
						auction.setRequsttime(new Date().getTime());
						auction.setIsupdatestate(true);
					}
				}
			}else if(auction.getState() == 2){//请求一次更新一次，这个字段长期没有更新，另外更新线程，将缓存中的内容更新
				auction.setRequsttime(new Date().getTime());
				auction.setIsupdatestate(false);
			}
		}else{
			auction = new Auction();
			auction.setId(aid);
			auction.setAuctionuname("陌路人");
			auction.setEndtime(new Date(new Date().getTime()+10*1000));
			auction.setRealprice(0);
			auction.setState(1);
			auction.setUptime(new Date().getTime());
			auction.setIsupdatestate(false);
		}
		return auction;
		
	}
	
	
	public void robot(Auction auction){
		Member robot =  getRandomRobot(auction);
		if(robot != null){ 
		//long currenttime =  this.getCurrenttime(auction);
 		int auctioncount = auction.getAuctioncount()+1;   //对数据库的竞拍数据更新
 		float currentprice = auction.getRealprice() + auction.getAddprice(); //当前价格
 		float robotspromote = auction.getRobotspromote()  + auction.getAddprice();
 		auction.setRobotspromote(CommonUtil.getScale2(robotspromote));
 		auction.setRealprice(CommonUtil.getScale2(currentprice));
 		auction.setAuctioncount(auctioncount);
 		auction.setEndtime(new Date(new Date().getTime() + 10*1000));
 		auction.setUid(robot.getId());
 		auction.setAuctionuname(robot.getName());
 		auction.setUptime(new Date().getTime());
 		//日志缓存
 		updateCacheLog(auction,robot);

 	   }
	}
	
	//更新竞拍日志缓存
	public void updateCacheLog(Auction auction,Member member){
		List<AuctionLog> aloglist =  AuctionCacheImpl.auctionLogCaches.get(auction.getId());
 		if(aloglist!=null){
 			if(aloglist.size()==10){
 				aloglist.remove(0);
 			}
 			AuctionLog alog = setAuctionLogData(auction,member);
 			aloglist.add(alog);
 		}else{
 			 aloglist = new LinkedList<AuctionLog>();
 			 AuctionLog alog = setAuctionLogData(auction,member);
 			 aloglist.add(alog);
 			 AuctionCacheImpl.auctionLogCaches.put(auction.getId(), aloglist);
 		}
	}
	
	public AuctionLog setAuctionLogData(Auction auction,Member member){
		AuctionLog alog = new AuctionLog();
		alog.setIp(member.getIp());
		alog.setName(member.getName());
		alog.setAid(auction.getId());
		alog.setUid(member.getId());
		alog.setBidprice(CommonUtil.getScale2(auction.getRealprice()));
		alog.setGoodsname(auction.getGoodsname());
		return alog;
	}
	
	//随机取用户
	public Member getRandomRobot(Auction auction){
		List<Member> robotlist = AuctionCacheImpl.robotCache;
		Member robot = null;
		if(robotlist.size() >= 3){
			int count = 0;
			int index = 0;
			do {
				count++;
				index = new Random().nextInt(robotlist.size());
				if(count == robotlist.size()){
					index = new Random().nextInt(3);
					count=0;
				}
				robot = robotlist.get(index);
			}while (robot.getId() == auction.getUid());
			return robot;
		}else{
			return null;
		}
	}
	
	
	//触发概率
	public boolean lucker(){
		  Random no = new Random();
		  int rno=no.nextInt(150);
		  if(rno>0 && rno<=88){
			  return true;
		  }else{
			  return false;
		  }
	}
	
}
